function Banner($)
{
	var _options = {
		loop:true,           // 是否循环
		anchor:true,         // 是否有指引
		prevNext:true,       // 前后切换按钮,暂时不实装，目前的样式中基本上不存在这个左右键
		anchorType:"circle", // 圆圈 :circle 直线:line
		swithTime:3000,      // 切换时间
		data:[],             // 数据{imgPath,url}
		afterComplete:function(){}, // 成功后的回调
		container:null
	};

	return {
		init:function(options){
			this.setting = $.extend({},_options,options);
			if(!this.setting.data || !this.setting.container){
				return;
			}
			var imgHtml = this.imgs(this.setting.data);
			var anchorHtml = this.anchors(this.setting.anchorType,this.setting.data.length);
			$(this.setting.container).html(imgHtml + anchorHtml);
			if(this.setting.afterComplete){
				this.setting.afterComplete.call();
			}
			$(this.setting.container).find("img").on("load",function(){
				var bgGround = "url("+ $(this).attr("tgsrc") + ") center center no-repeat";
				$(this).prev().css({"background":bgGround});
				$(this).prev().css({"background-size":"cover"});
				$(this).parent().parent().removeClass("loading");
				$(this).parent().removeClass("loading");
			});
			this.curIndex = 1;
			this.step = 1;
			this.allCnt = this.setting.data.length;
			this.jumpTo(this.curIndex);
			if(this.allCnt >1){
				if(this.setting.loop){
					this.loop(this.setting.swithTime);
				}
				this.events();
			}
			
			return this;
		},
		imgs:function(data){
			var html = '<ul class="banner-items">';
			$(data).each(function(idx){
				html += '<li class="banner-item loading" tg="' + (idx+1) + '" >';
				var img = '<div /> <img style="display:none;" tgsrc="' + this.imgPath + '" src="' + this.imgPath + '" />'
				if(this.url){
					html += '<a target="blank" href="' + this.url + '" >';
					html += img;
					html += '</a>';
				}else{
					html += img;
				}
				html += '</li>';	
			});
			html += '</ul>';		
			return html;
		},
		anchors:function(type,length){
			var html = '<ul class="banner-anchors">';
			var type = type=='circle'?'':'line';
			for(var i=1;i <= length;i++){
				html += '<li tg="'+ i +'" class="anchor ' + type + '"></li>'
			}
			html += '</ul>';
			return html;
		},
		prevNext:function(){

		},
		loop:function(swithTime){
			var _this = this;
			_this.interval = setInterval(function(){
				if(!_this.pausing){
					var endVal = _this.allCnt;
					if(_this.step < 0){
						endVal = 0;
					}
					if( (_this.step < 0 ) && _this.curIndex + _this.step > endVal 
						|| (_this.step > 0 ) && _this.curIndex + _this.step <= endVal ){
						_this.curIndex += _this.step;
					}else{
						_this.curIndex = _this.step?(_this.allCnt - 1):2;
						_this.step = _this.step * -1;
					}
					_this.jumpTo(_this.curIndex);
				}
			},swithTime);
		},
		stopInterval:function(){
			var _this = this;
			clearInterval(_this.interval);	
		},
		resume:function(curIdx,swithTime){
			var _this = this;
			if(curIdx == _this.allCnt){
				_this.step = -1;
			}else if(curIdx == 1){
				_this.step = 1;
			}
			_this.loop(swithTime);
		},
		jumpTo:function(idx){
			var _this = this;
			_this.curIndex = parseInt(idx);
			$(_this.setting.container).find(".banner-item").removeClass("cur");
			$(_this.setting.container).find(".anchor").removeClass("cur");
			$(_this.setting.container).find(".banner-item[tg='" + idx + "']").addClass("cur");
			$(_this.setting.container).find(".anchor[tg='" + idx + "']").addClass("cur");
		},
		events:function(){
			var _this = this;
			$(_this.setting.container).find(".anchor").hover(function(){
				_this.pausing = true;
				_this.stopInterval();
				_this.jumpTo($(this).attr("tg"));
			},function(){
				_this.pausing = false;
				_this.resume($(this).attr("tg"),_this.setting.swithTime);
			});
		}
	};
}

if("function"==typeof define && define.amd && define){
	define(['jquery'],function($){
		return Banner($);
	});
}